# -*- coding: utf-8 -*-
# Author: xiantingDeng
# File: base_driver.py
# Time: 17:18

import yaml
from appium import webdriver


class BaseDriver:

    def __init__(self, device_info):
        self.device_info = device_info
        # cmd = "start appium -p {0} -bp {1} -U 127.0.0.1:{2}".format(self.device_info["server_port"], self.device_info["server_port"], self.device_info["device_port"])
        # os.system(cmd)

    def base_driver(self, automationName="appium"):
        fs = open("caps.yaml")
        # 平台名称、包名、Activity名称、超时时间、是否重置、server_ip、
        desired_caps = yaml.load(fs, Loader=yaml.FullLoader)
        # 版本信息
        desired_caps["platform_version"] = self.device_info["platform_version"]
        # 设备名称
        desired_caps["deviceName"] = self.device_info["device_port"]  # f"127.0.0.1:{self.device_info['device_port']}"
        # 系统端口号
        desired_caps["systemPort"] = self.device_info["system_port"]
        desired_caps["udid"] = self.device_info["device_port"]
        # print(desired_caps)

        if automationName != "appium":
            desired_caps["automationName"] = automationName
        print(f'设备{self.device_info["device_port"]}启用appium参数为：{desired_caps}')
        driver = webdriver.Remote(f"http://127.0.0.1:{self.device_info['server_port']}/wd/hub",
                                  desired_capabilities=desired_caps)
        return driver
